#include "inspector.h"

inspector::inspector(QWidget * parent) :QTreeWidget (parent)
{
	setColumnCount (2);
	setHeaderLabels(QStringList() << "" << "" );
}
inspector::~inspector(void){
}

void inspector::show(box *b){
	clear();
	addTopLevelItem (new QTreeWidgetItem(QStringList() << "Name" << b->name));
	addTopLevelItem (new QTreeWidgetItem(QStringList() << "Number" << QString::number(b->number)));
	addTopLevelItem (new QTreeWidgetItem(QStringList() << "Cost" << QString::number(b->cost)));
	//addTopLevelItem (new QTreeWidgetItem(QStringList() << "Cost unit" << b->cost_unit));
	addTopLevelItem (new QTreeWidgetItem(QStringList() << "Links"));
	for(int i=0; i < b->links.size(); i++){
		QString type;
		if (b->links[i]->from->b == b){ 
			topLevelItem(3)->addChild(new QTreeWidgetItem(QStringList() << b->links[i]->name << QString("Output")));
		}
		else{
			QList<link::end*> ends = b->links[i]->to.values(b->id);
			for (int j = 0; j<ends.size(); j++)
				topLevelItem(3)->addChild(new QTreeWidgetItem(QStringList() << b->links[i]->name << atypeToString(ends[j]->type)));
		}
	}
}
void inspector::show(link *lnk){
	clear();
	addTopLevelItem (new QTreeWidgetItem(QStringList() << "Name" << lnk->name));
	QString name("...");
	if (lnk->from->b!=NULL) name = lnk->from->b->name;
	addTopLevelItem (new QTreeWidgetItem(QStringList() << "From" << name));
	addTopLevelItem (new QTreeWidgetItem(QStringList() << "To..." ));
	QList<link::end*> ends = lnk->to.values();
	for(int i=0; i < ends.size(); i++){
		topLevelItem(2)->addChild(new QTreeWidgetItem(QStringList() << ((ends[i]->b==NULL) ? QString("...") : ends[i]->b->name) << atypeToString(ends[i]->type)));
	}
}
QString inspector::atypeToString(link::ArrowType type){
	switch (type){
		case link::Control: return QString("Control");
		case link::Input: return QString("Input");
		case link::Output: return QString("Output");
		case link::Resource: return QString("Resource");
	}
	return QString();
}

